package com.jackchong.net

import com.jackchong.base.BaseView
import com.dpb.tuangou.ext.lifeRecycle
import kotlinx.coroutines.*

/**
 * author : chong.huang
* create : 2/14/21
 * desc   :
 */
class CommonLoading(private val loadingMode: NetLoadingMode, private val view: BaseView) : Loading() {

    private lateinit var loadingJob: Job

    override fun create() {
        loadingJob = CoroutineScope(Dispatchers.Main).launch {
            val delayMs = when (loadingMode) {
                NetLoadingMode.SHOW -> 0L
                NetLoadingMode.DELAY -> 500L
                NetLoadingMode.HIDE -> -1L
            }
            if (delayMs != -1L){
                delay(delayMs)
                view.showLoading()
            }
        }.lifeRecycle(view.lifecycle())
    }

    override fun cancel() {
        view.hideLoading()
        loadingJob.cancel()
    }
}